﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.MarketData
{
    public class MarketDataHelper
    {
        static public string ConvertGBXGBpToGBP(string currency,out bool ret)
        {
            ret = false;
            if (currency == MarketDataConstants.GBp || currency == MarketDataConstants.GBX)
            {
                ret = true;
                return MarketDataConstants.GBP;
            }
            else
                return currency; 
        }
         
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tick"></param>
        /// <returns></returns>
        static public int GetDecimalPlacesFromPriceMovement(double tick)
        {
            int decimalPlace = 1;

            string data = tick.ToString("0.##########");

            // State variables.
            if ( data.IndexOf('.') == -1)
                return decimalPlace;

            int len = data.Length -1;

            bool foundNonZero = true;

            decimalPlace = 0;
            
            while (len >= 0)
            {
                char ch = data[len];

                if (ch == '0')
                {
                    if (foundNonZero == true)
                        decimalPlace++;
                }
                else if (char.IsDigit(ch))
                {
                    // Found a non-zero; increment the decimal places.
                    foundNonZero = true;
                    decimalPlace++;
                }
                else if (ch == '.')
                {
                    // Found a decimal point; return the decimal places.
                    return decimalPlace;
                }
                len--;
            }

            return 0;
        }

    }
}
